<template>
    <div>
        <van-nav-bar left-arrow @click-left="onClickLeft()" title="用户登录"></van-nav-bar>
        <div class="wrap">
            <div class="form-content">
                <div class="form-title">
                    <div>Welcome To </div>
                    <div style="margin-left:10px;"><van-image src="https://create.nightowl.top/static/images/logo.png"
                            height="40" width="40"></van-image></div>
                    <div>易搭衣橱</div>
                </div>
                <van-form @submit="onSubmit">
                    <van-cell-group inset>
                        <van-field icon-prefix="iconfont" :clearable="true" left-icon="user" v-model="username"
                            label-width="60px" name="username" label="用户名" placeholder="请输入用户名~"
                            label-class="field-username" :rules="[{ required: true, message: '(°ー°〃)' }]">
                            <template #left-icon>
                                <van-icon class-prefix="iconfont" name="user" color="#f3a8df" size="20" /></template>
                        </van-field>
                        <van-field v-model="password" icon-prefix="iconfont" :clearable="true" left-icon="password"
                            label-width="60px" type="password" name="password" label="密码" placeholder="请输入密码~"
                            :rules="[{ required: true, message: '(°ー°〃)' }]" label-class="field-password">
                            <template #left-icon>
                                <van-icon class-prefix="iconfont" name="password" color="#37e962" size="20" /></template>
                        </van-field>
                    </van-cell-group>
                    <div style="margin: 16px;">
                        <van-button round block type="primary" native-type="submit">提交</van-button>
                    </div>
                    <div class="text-link">
                        <div @click="navToPage('/register')">注册账号</div>
                        <div @click="forgetPassword">忘记密码？</div>
                    </div>
                </van-form>
            </div>
            <div class="buttom-box">
                <div class="login-type">
                    <div class="wechat item" @tap="loginByWeChat()">
                        <div class="icon">
                            <van-icon class-prefix="iconfont" name="weixin" size="40" color="#53c240"></van-icon>
                        </div>
                        <div>微信</div>
                    </div>
                    <div class="QQ item" @click="loginByQQ()">
                        <div class="icon">
                            <van-icon class-prefix="iconfont" name="qq" size="40" color="#11b7e9"></van-icon>
                        </div>
                        QQ
                    </div>
                </div>
                <!-- <div class="hint">
                    提示：备注：获取QQ信息用于个人信息找回及其他反馈信息^_^!
                </div> -->
            </div>
        </div>
    </div>
</template>
  
<script setup>
import { ref } from "vue";
import { showDialog, showToast, } from "vant";
import { useRouter } from 'vue-router';
import _service from "../request/axios";
const username = ref('');
const password = ref('');
const router = useRouter();
function onClickLeft() {
    router.back();
}
function navToPage(route) {
    router.push({
        path: route
    });
}
function forgetPassword() {
    showDialog({
        title: '温馨提示',
        message: '本软件暂时还没有足够的经费接入有效的短信验证，为了保护您账号的安全，您可以联系软件管理员的QQ（2889797849）,申请重置密码哦，非常感谢您的理解~',
    }).then(() => {
        // on close
    });
}
const onSubmit = (values) => {
    if (username.value.length < 6 || password.value.length < 6) {
        showToast("用户名或密码长度过短~");
        return;
    }
    _service.post("/user/login", values).then(res => {
        if (res) {
            if (!res.code && res.data.token) {
                showToast("登陆成功~");
                //写入用户数据缓存
                localStorage.setItem("token", JSON.stringify(res.data.token));
                localStorage.setItem("userInfo", JSON.stringify(res.data));
                router.back();
            } else {
                showToast(res.msg);
            }
        } else {
            showToast("服务端错误，请稍后再试~")
        }
        // console.log(res)
    })
};
</script>
  
<style lang="less">
.form-content {
    padding: 20px;
    box-sizing: border-box;

    .form-title {
        width: 100%;
        text-align: center;
        font-size: 40rpx;
        font-weight: bold;
        margin-bottom: 200rpx;
        color: #87abf2;
        margin-bottom: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .field-username {
        color: #f3a8df
    }

    .field-password {
        color: #37e962
    }

    .text-link {
        display: flex;
        color: #b9bdbe;
        padding: 0px 20px;
        box-sizing: border-box;
        justify-content: space-between;
        font-size: 14px;
    }
}

.buttom-box {
    padding: 0px 35px;

    .login-type {
        display: flex;
        justify-content: space-between;
    }

    .hint {
        margin-top: 20px;
        text-align: center;
        font-size: 12px;
        color: #3792ed92;
        text-align: left;
    }
}

.van-cell__value {
    justify-content: flex-start;
}

.van-field__control {
    max-width: 100px;
}

.van-field {
    align-items: center;
}
</style>
  